//
//  VisbleView.swift
//  我的微博
//
//  Created by 吴加将 on 15/9/6.
//  Copyright © 2015年 吴加将. All rights reserved.
//

import UIKit
protocol visbleViewDelegate : NSObjectProtocol{
    func visbleViewWillRegister();
    func visbleViewWillLoggin();
}

class VisbleView: UIView {
    weak var delegate : visbleViewDelegate?
    //注册按钮的点击事件
    func clickRegister(){
        delegate?.visbleViewWillRegister()
    }
    //登录按钮的点击事件
    func clickLoggin(){
        delegate?.visbleViewWillLoggin()
    }
    override init(frame:CGRect){
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        setUpUI()
    }
    //设置试图
    func setUpInfo(isHome:Bool,name:String,text:String){
        iconView.image=UIImage(named: name)
        textlabel.text=text
        if !isHome{
            homeView.hidden=true
            sendSubviewToBack(maskview)
        }else{
            setAnimation()
        }
    }
    
    
    
    
    
    
    //设置动画
    private func setAnimation(){
    let anim=CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue=2*M_PI
        anim.repeatCount=MAXFLOAT
        anim.duration=20.0
        anim.removedOnCompletion=false
    iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    
   private func setUpUI(){
        //添加控件
        addSubview(iconView)
        addSubview(maskview)
        addSubview(homeView)
        addSubview(textlabel)
        addSubview(registerBtn)
        addSubview(logginBtn)
        // MARK: - 设置frame

        //设置iconview的frame
        iconView.translatesAutoresizingMaskIntoConstraints=false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        //设置homeview的frame
        homeView.translatesAutoresizingMaskIntoConstraints=false
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //设置label的frame
        textlabel.translatesAutoresizingMaskIntoConstraints=false
        addConstraint(NSLayoutConstraint(item: textlabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: textlabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        //设置文字的宽度
         addConstraint(NSLayoutConstraint(item: textlabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        //设置btn
        registerBtn.translatesAutoresizingMaskIntoConstraints=false
         addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textlabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textlabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        logginBtn.translatesAutoresizingMaskIntoConstraints=false
        addConstraint(NSLayoutConstraint(item: logginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textlabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: logginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textlabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: logginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: logginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        //遮罩视图
        maskview.translatesAutoresizingMaskIntoConstraints=false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskview" : maskview]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskview]-(-35)-[logginBtn]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views:["maskview":maskview,"logginBtn":logginBtn]))
        
        //设置背景
        backgroundColor=UIColor(white: 237.0/255.0, alpha: 1.0)


       
        
    }
    
    
    
    
    
    
    
    
    
    
    //懒加载图标
   private lazy var iconView : UIImageView = {
      let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    //懒加载遮盖
  private  lazy var maskview : UIImageView = {
        let mv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return mv
        }()
    //懒加载home
  private  lazy var homeView : UIImageView = {
        let hv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return hv
    }()
    //懒加载label
   private lazy var textlabel : UILabel = {
        let tl = UILabel()
        tl.text="关注一些人，回这里看看有什么惊喜"
        tl.tintColor=UIColor.darkGrayColor()
        tl.numberOfLines=0
        tl.textAlignment=NSTextAlignment.Center
        tl.font=UIFont.systemFontOfSize(15)
        tl.sizeToFit()
        
    return tl
    }()
    //懒加载RegisterBtn
  private  lazy var registerBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)

        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action:"clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //懒加载LogginBtn
 private   lazy var logginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action:"clickLoggin", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    
    
    
    
    
    
  
}
